﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace 股票测试
{
    /// <summary>
    /// 股票帮助类
    /// </summary>
    class ShareHelper
    {
        /// <summary>
        /// 接入网址
        /// </summary>
        public static string URL { get; set; }

        /// <summary>
        /// URL参数
        /// </summary>
        public static string URLParam { get; set; }

        /// <summary>
        /// 结果
        /// </summary>
        public static string Result { get; set; }

        /// <summary>
        /// 请求网页，返回结果以字符串返回
        /// </summary>
        /// <returns></returns>
        public static string GetShareInfo()
        {
            try
            {
                URL = "http://hq.sinajs.cn/list=";
                WebRequest request = WebRequest.Create(URL + URLParam);
                WebResponse response = request.GetResponse();
                Stream stream = response.GetResponseStream();
                StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("gb2312"));
                string responseString = sr.ReadToEnd();
                sr.Close();
                stream.Close();
                return responseString;
            }
            catch (Exception e)
            {
                return "发现错误：" + e.Message;
            }
        }

        /// <summary>
        /// 处理股票信息
        /// </summary>
        public static void ProcessResponse()
        {
            Result = string.Empty;
            string response = GetShareInfo();
            string[] groupShares = response.Split(';');

            for (int i = 0; i < groupShares.Length; i++)
            {
                string singleShares = groupShares[i];

                if (!singleShares.Contains(','))
                {
                    continue;
                }

                string[] array= singleShares.Split(',');

                try
                {
                    double buyNum1 = Convert.ToDouble(array[10]) / 100;
                    double currentPrice = Convert.ToDouble(array[3]);
                    double yestodayPrice = Convert.ToDouble(array[2]);

                    Result += "名称:" + array[0].Remove(0, 21).Replace('"',' ') + Environment.NewLine;
                    Result += "当前价格:" + array[3] + Environment.NewLine;
                    Result += "涨幅：" + Math.Round((currentPrice - yestodayPrice) / yestodayPrice, 4) * 100 + "%" + Environment.NewLine;
                    Result += "涨跌:" + Math.Round((currentPrice - yestodayPrice), 2) + Environment.NewLine;
                    Result += "今日开盘价:" + array[1] + Environment.NewLine;
                    Result += "昨日收盘价:" + array[2] + Environment.NewLine;
                    Result += "今日最高价:" + array[4] + Environment.NewLine;
                    Result += "今日最低价:" + array[5] + Environment.NewLine;
                    Result += "买一(手):" + buyNum1 + Environment.NewLine;
                    Result += "时间:" + array[31] + Environment.NewLine;
                    Result += "------------------------------------------------" + Environment.NewLine;
                }
                catch (Exception e)
                {

                }
            }

            Console.WriteLine(Result);
        }
    }
}
